package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Type;
import com.bwie.service.TypeService;
import com.bwie.mapper.TypeMapper;
import com.bwie.utils.Result;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.netty.util.internal.UnstableApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author 陶仕豪
* @description 针对表【tb_type】的数据库操作Service实现
* @createDate 2025-05-24 10:28:19
*/
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type>
    implements TypeService{
    @Autowired
    TypeMapper typeMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public Result addType(Type type) {

        String token = request.getHeader("token");
        if(token==null){
            return Result.error().setMsg("请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);
        type.setCreateBy(nickName);
        type.setCreateTime(new Date());
        typeMapper.insert(type);
        return Result.ok();
    }

    @Override
    public Result deleteType(Integer typeId) {

        //判断分类是否存在
        Type type = typeMapper.selectById(typeId);
        if (type==null){
            return Result.error().setMsg("分类不存在");
        }

        //删除分类
        typeMapper.deleteById(typeId);
        return Result.ok();
    }

    @Override
    public Result getTypeTreeList() {

        //获取所有分类
        List<Type> listAll = typeMapper.selectList(new QueryWrapper<Type>().lambda().orderByAsc(Type::getTypeTop));
        List<Tree<String>> trees = TreeUtil.build(listAll, "0", (node, tree) -> {
            tree.setId(node.getTypeId() + "");
            tree.setParentId(node.getTypePid() + "");
            tree.setName(node.getTypeName());
            tree.putExtra("typeImage", node.getTypeImage());
            tree.putExtra("typeStatus", node.getTypeStatus());
            tree.putExtra("typeTop", node.getTypeTop());
            tree.putExtra("createTime",node.getCreateTime());
        });
        return Result.ok().setData(trees);
    }

    @Override
    public Result setTypeStatus(Integer typeId, Integer typeStatus) {
        //获取分类信息
        Type type = typeMapper.selectById(typeId);
        if(type==null){
            return Result.error().setMsg("分类不存在");
        }

        //更新分类状态
        type.setTypeStatus(typeStatus);
        typeMapper.updateById(type);
        return Result.ok();
    }

    @Override
    public Result deleteTypeById(Integer typeId) {
        //获取分类信息
        Type type = typeMapper.selectById(typeId);
        if(type==null){
            return Result.error().setMsg("分类不存在");
        }
        //删除分类
        typeMapper.deleteById(typeId);
        return Result.ok();
    }

    @Override
    public Result uploadFile(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return Result.ok().setData(url);
        } catch (IOException e) {
           e.printStackTrace();
            return Result.error().setMsg("上传失败");
        }

    }

    @Override
    public Result updateType(Type tbType) {
        //获取分类信息
        Type type = typeMapper.selectById(tbType.getTypeId());
        if(type==null){
            return Result.error().setMsg("分类不存在");
        }
        //更新分类信息
        typeMapper.updateById(tbType);
        return Result.ok();
    }

    @Override
    public Result getTypeSelectTreeList() {
        //获取所有分类
        List<Type> listAll = typeMapper.selectList(new QueryWrapper<Type>().lambda().orderByAsc(Type::getTypeTop));
        List<Tree<String>> trees = TreeUtil.build(listAll, "0", (node, tree) -> {
            tree.setId(node.getTypeId() + "");
            tree.setParentId(node.getTypePid() + "");
            tree.setName(node.getTypeName());
            tree.putExtra("label",node.getTypeName());
            tree.putExtra("value",node.getTypeId());
            tree.putExtra("typeImage", node.getTypeImage());
            tree.putExtra("typeStatus", node.getTypeStatus());
            tree.putExtra("typeTop", node.getTypeTop());
            tree.putExtra("createTime",node.getCreateTime());
        });
        return Result.ok().setData(trees);
    }
}




